package cn.iphone.qqspeak;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import cn.iphone.qqspeak.utils.Constant;

public class MainMenuActivity extends Activity implements OnItemClickListener{
	private static final String TAG = "MainMenuActivity";
	private static final boolean DEBUG = true;
	
	private GridView lv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.mainmenu);
		setupView();
	}

	private void setupView() {
		lv = (GridView) findViewById(R.id.gv_menu);
		lv.setAdapter(new MainMenuAdapter(this));
		lv.setOnItemClickListener(this);
	    
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}


	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	private class MainMenuAdapter extends BaseAdapter {
		private LayoutInflater inflater;

		private int[] iconArr = { R.drawable.logo_aiqing, R.drawable.logo_gexing,
				R.drawable.logo_qinggan, R.drawable.logo_jieri, R.drawable.logo_zheli,
				R.drawable.logo_qita};

		public MainMenuAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return iconArr.length;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup group) {
			ViewHolder holder = null;

			if (view == null) {
				holder = new ViewHolder();
				view = inflater.inflate(R.layout.gv_item, null);

				holder.imgView = (ImageView) view
						.findViewById(R.id.gv_item_icon);
				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag();
			}

			holder.imgView.setBackgroundResource(iconArr[position]);

			return view;
		}
	}

	static class ViewHolder {
		public ImageView imgView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent(getBaseContext(),ContentActivity.class);
		switch (position) {
		case 0:
			intent.putExtra(Constant.TYPE, Constant.CATA_AIQING);
			break;
		case 1:
			intent.putExtra(Constant.TYPE, Constant.CATA_GEXING);
			break;
		case 2:
			intent.putExtra(Constant.TYPE, Constant.CATA_QINGGAN);
			break;
		case 3:
			intent.putExtra(Constant.TYPE, Constant.CATA_JIERI);
			break;
		case 4:
			intent.putExtra(Constant.TYPE, Constant.CATA_ZHELI);
			break;
		case 5:
			intent.putExtra(Constant.TYPE, Constant.CATA_QITA);
			break;
		}
		startActivity(intent);
		finish();
	}

}
